package 力扣日常刷题.木22二月.第14天220;

import java.util.LinkedList;

/**
 * @author 帅小伙
 * @date 2022/2/20
 * @description
 */
public class Demo111字符串解码 {

    public String decodeString(String s) {
        // 保存数和符号的
        StringBuilder res = new StringBuilder();
        int multi = 0;
        // 一个数栈  一个符号栈
        LinkedList<Integer> stack_multi = new LinkedList<>();
        LinkedList<String> stack_res = new LinkedList<>();

        for (char c : s.toCharArray()) {
            if(c == '[') {
                stack_multi.addLast(multi);
                stack_res.addLast(res.toString());
                multi = 0;
                res = new StringBuilder();
            } else if(c == ']') {
                StringBuilder t = new StringBuilder();
                int count = stack_multi.removeLast();
                for (int i = 0; i < count; i++) t.append(res);
                res = new StringBuilder(stack_res.removeLast() + t);
            } else if(c >='0' && c <= '9') multi = multi * 10 + c - '0';
            else res.append(c);
        }
        return res.toString();
    }

}
